<%@ page language="java" errorPage="../error/exceptionHandler.jsp" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*" %>
<%@ page import="org.apache.log4j.Logger" %>
<%@ page import="org.cgs.web.database.DatabaseConnector" %>
<%
        Logger LOGGER = Logger.getLogger("student.jsp");
	String courseCode = "";
	String id = "";
	String name = "";
	String studentNumber = "";
	String email = "";
	String phone = "";
	String programAndYear = "";
	String recentCourse = "";
	String whyInterested = "";
	String likeSubject = "";
	String transportation = "";
	String teachingExperience = "";
	String teachingAttraction = "";
	String hasVehicle="";
	String hasTeachingExperience = "";
	String hasSubjectKnowledge = "";
	boolean foundStudent = false;
	Set checkedTimes = new HashSet();
	String msg = "";

	// Get page parameters
	if(request.getParameter("courseCode") != null)
	{
	     courseCode = request.getParameter("courseCode");
        }	
	if(request.getParameter("id") != null)
	{
	     id = request.getParameter("id");
        }

        Connection con = null;
	PreparedStatement pstmt = null;
	ResultSet rs = null;
     	PreparedStatement pstmt2 = null;
	ResultSet rs2 = null;

	try
	{
        	con = DatabaseConnector.getInstance().getConnection();
	        pstmt = con.prepareStatement("SELECT * FROM student WHERE studentNumber=? AND courseCode=?");
		pstmt.setString(1, id);
		pstmt.setString(2, courseCode);
	        rs = pstmt.executeQuery();

 	        if(rs.next())
		{
		       foundStudent = true;
		       name = rs.getString("name");
		       studentNumber = rs.getString("studentNumber");
		       email = rs.getString("email");
		       phone = rs.getString("phone");
		       hasVehicle = rs.getString("hasVehicle");
		       hasTeachingExperience = rs.getString("hasTeachingExperience");
		       hasSubjectKnowledge = rs.getString("hasSubjectKnowledge");
	       }


	        pstmt2 = con.prepareStatement("SELECT * FROM student_times WHERE coursecode=? AND studentNumber=?");
		pstmt2.setString(1, courseCode);
                pstmt2.setString(2, id);
	        rs2 = pstmt2.executeQuery();
		while (rs2.next())
		{
			checkedTimes.add(rs2.getString("time"));
		} //end while loop
	    }
	    catch(Exception e)
	    {
                LOGGER.error("Exception thrown while atttempting to edit a student", e);
	    }
	    finally
	    {
            	try
            	{
                  // Close the ResultSet
                  if (rs != null)
                  {
                     rs.close();
                  }
            	}
            	catch (Exception e)
            	{
                    LOGGER.error("Exception cleaning up result set", e);
            	}
		
		try
             	{
                    // Close the ResultSet
                    if (rs2 != null)
                    {
                       rs2.close();
                    }
            	}
            	catch (Exception e)
            	{
                    LOGGER.error("Exception cleaning up result set", e);
            	}
	  
		try
            	{
                   // Close the PreparedStatement
                   if (pstmt != null)
                   {
                       pstmt.close();
                    }
            	}
            	catch (Exception e)
            	{
                    LOGGER.error("Exception cleaning up prepared statement", e);
            	}
	
		try
            	{
                    // Close the PreparedStatement
                    if (pstmt2 != null)
                    {
                       pstmt2.close();
                    }
                }
                catch (Exception e)
                {
                    LOGGER.error("Exception cleaning up prepared statement", e);
                }
	     
		try
            	{
                   // Close the connection
                   if (con != null)
                   {
                      con.close();
                   }
            	}
            	catch (Exception e)
            	{
                   LOGGER.error("Exception cleaning up connection to the database", e);
            	}
	    }
	%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en-AU">
  <head>
    <meta http-equiv="content-type" content="application/xhtml+xml; charset=UTF-8" />
    <link rel="stylesheet" type="text/css" href="../css/sinorca-screen.css" media="screen" title="Sinorca (screen)" />
    <link rel="stylesheet alternative" type="text/css" href="../css/sinorca-screen-alt.css" media="screen" title="Sinorca (alternative)" />
    <link rel="stylesheet" type="text/css" href="../css/sinorca-print.css" media="print" />
    <link rel="stylesheet" type="text/css" href="../css/sinorca.css" />

    <title>Instructor Students Page</title>
  </head>

  <body>
    <div id="top"><a href="#main-copy" class="doNotDisplay doNotPrint">Skip to main content.</a></div>
    <div id="header">
      <div class="superHeader">
      </div>

      <div class="midHeader">
        <h1 class="headerTitle">Course Group Scheduler 3.0</h1>
      </div>
    </div>

    <!-- ##### Side Bar ##### -->
    <div id="side-bar">
      <div>
        <p class="sideBarTitle">Students Menu</p>
        <ul>
	  <li><a href="addStudent.jsp?courseCode=<%= courseCode %>">Add Student</a></li>
        </ul>
      </div>
    </div>

    <!-- ##### Main Copy ##### -->

    <div id="main-copy">
      <h1 id="introduction">Student Data</h1>
      <form name="f" method="post" action="/cgs/EditStudent">
	      <INPUT type="hidden" name="courseCode" value="<%=courseCode%>">
	      <INPUT type="hidden" name="isInstructor" value="false">
<%
	    if(foundStudent)
	    {
	    %>
	<table>
	<tr><td>Student Name:</td><td><INPUT TYPE="text" class="textInput" NAME="name" VALUE="<%= name %>"></td></tr>
	<tr><td>Student Number:</td><td><INPUT TYPE="text" class="textInput" NAME="studentNumber" VALUE="<%= studentNumber %>"></td></tr>
	<tr><td>Phone:</td><td><INPUT TYPE="text" class="textInput" NAME="phone" VALUE="<%=phone%>"></td></tr>
	<tr><td>Email:</td><td><INPUT TYPE="text" class="textInput" NAME="email" VALUE="<%=email%>"></td></tr>
	<tr><td>Program and Year:</td><td><INPUT TYPE="text" class="textInput" NAME="programAndYear" VALUE="<%=programAndYear%>"></td></tr>
	<tr><td>Recent Course:</td><td><textarea name="recentCourse"><%=recentCourse%></textarea></td></tr>
	<tr><td>Interest In This Course:</td><td><textarea name="whyInterested"><%=whyInterested%></textarea></td></tr>
	<tr><td>Subject Likes/Dislikes:</td><td><textarea name="likeSubject"><%=likeSubject%></textarea></td></tr>
	<tr><td>Available Transportation:</td><td><textarea name="transportation"><%=transportation%></textarea></td></tr>
	<tr><td>Teaching Experience:</td><td><textarea name="teachingExperience"><%=teachingExperience%></textarea></td></tr>
	<tr><td>Teaching Attractions:</td><td><textarea name="teachingAttraction"><%=teachingAttraction%></textarea></td></tr>
	<tr></tr>
	<%
	if(hasVehicle.equalsIgnoreCase("true"))
	{
	%>
	<tr><td>Has Vehicle:</td><td><select name="hasVehicle"><option value="true">True</option><option value="false">False</option></select></td></tr>
        <%
	}
	else
	{
	%>
	<tr><td>Has Vehicle:</td><td><select name="hasVehicle"><option value="false">False</option><option value="true">True</option></select></td></tr>
	<%
	}
	%>
	<%
	if(hasTeachingExperience.equalsIgnoreCase("true"))
	{
	%>
	<tr><td>Has Teaching Experience:</td><td><select name="hasTeachingExperience"><option value="true">True</option><option value="false">False</option></select></td></tr>
        <%
	}
	else
	{
	%>
	<tr><td>Has Teaching Experience:</td><td><select name="hasTeachingExperience"><option value="false">False</option><option value="true">True</option></select></td></tr>
	<%
	}
	%>
	<%
	if(hasSubjectKnowledge.equalsIgnoreCase("true"))
	{
	%>
	<tr><td>Has Subject Knowledge:</td><td><select name="hasSubjectKnowledge"><option value="true">True</option><option value="false">False</option></select></td></tr>
        <%
	}
	else
	{
	%>
	<tr><td>Has Subject Knowledge:</td><td><select name="hasSubjectKnowledge"><option value="false">False</option><option value="true">True</option></select></td></tr>
	<%
	}
	%>
	</table>
	<br>
	<br>
	
	Student busy times:<br>
	<table>
				<tr>
					<td>&nbsp;</td>
					<td>Monday</td>
					<td>Tuesday</td>
					<td>Wednesday</td>
					<td>Thursday</td>
					<td>Friday</td>
				</tr>
				<tr>
					<td>8:30-9:00AM</td>
					<% msg = new String(); if(checkedTimes.contains("Mon0830")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Mon0830" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Thu0830")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Tue0830" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Wed0830")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Wed0830" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Thu0830")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Thu0830" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Fri0830")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Fri0830" <%= msg %>></td>
				</tr>
				<tr>
					<td>9:00-9:30AM</td>
					<% msg = new String(); if(checkedTimes.contains("Mon0900")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Mon0900" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Tue0900")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Tue0900" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Wed0900")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Wed0900" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Thu0900")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Thu0900" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Fri0900")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Fri0900" <%= msg %>></td>
				</tr>
				<tr>
					<td>9:30-10:00AM</td>
					<% msg = new String(); if(checkedTimes.contains("Mon0930")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Mon0930" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Tue0930")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Tue0930" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Wed0930")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Wed0930" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Thu0930")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Thu0930" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Fri0930")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Fri0930" <%= msg %>></td>
				</tr>
				<tr>
					<td>10:00-10:30AM</td>
					<% msg = new String(); if(checkedTimes.contains("Mon1000")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Mon1000" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Tue1000")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Tue1000" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Wed1000")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Wed1000" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Thu1000")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Thu1000" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Fri1000")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Fri1000" <%= msg %>></td>
				</tr>
				<tr>
					<td>10:30-11:00AM</td>
					<% msg = new String(); if(checkedTimes.contains("Mon1030")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Mon1030" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Tue1030")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Tue1030" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Wed1030")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Wed1030" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Thu1030")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Thu1030" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Fri1030")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Fri1030" <%= msg %>></td>
				</tr>
				<tr>
					<td>11:00-11:30AM</td>
					<% msg = new String(); if(checkedTimes.contains("Mon1100")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Mon1100" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Tue1100")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Tue1100" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Wed1100")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Wed1100" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Thu1100")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Thu1100" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Fri1100")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Fri1100" <%= msg %>></td>
				</tr>
				<tr>
					<td>11:30-12:00PM</td>
					<% msg = new String(); if(checkedTimes.contains("Mon1130")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Mon1130" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Tue1130")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Tue1130" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Wed1130")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Wed1130" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Thu1130")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Thu1130" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Fri1130")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Fri1130" <%= msg %>></td>
				</tr>
				<tr>
					<td>12:00-12:30PM</td>
					<% msg = new String(); if(checkedTimes.contains("Mon1200")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Mon1200" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Tue1200")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Tue1200" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Wed1200")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Wed1200" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Thu1200")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Thu1200" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Fri1200")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Fri1200" <%= msg %>></td>
				</tr>
				<tr>
					<td>12:30-1:00PM</td>
					<% msg = new String(); if(checkedTimes.contains("Mon1230")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Mon1230" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Tue1230")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Tue1230" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Wed1230")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Wed1230" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Thu1230")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Thu1230" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Fri1230")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Fri1230" <%= msg %>></td>
				</tr>
				<tr>
					<td>1:00-1:30PM</td>
					<% msg = new String(); if(checkedTimes.contains("Mon0100")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Mon0100" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Tue0100")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Tue0100" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Wed0100")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Wed0100" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Thu0100")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Thu0100" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Fri0100")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Fri0100" <%= msg %>></td>
				</tr>
				<tr>
					<td>1:30-2:00PM</td>
					<% msg = new String(); if(checkedTimes.contains("Mon0130")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Mon0130" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Tue0130")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Tue0130" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Wed0130")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Wed0130" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Thu0130")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Thu0130" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Fri0130")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Fri0130" <%= msg %>></td>
				</tr>
				<tr>
					<td>2:00-2:30PM</td>
					<% msg = new String(); if(checkedTimes.contains("Mon0200")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Mon0200" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Tue0200")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Tue0200" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Wed0200")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Wed0200" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Thu0200")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Thu0200" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Fri0200")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Fri0200" <%= msg %>></td>
				</tr>
				<tr>
					<td>2:30-3:00PM</td>
					<% msg = new String(); if(checkedTimes.contains("Mon0230")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Mon0230" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Tue0230")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Tue0230" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Wed0230")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Wed0230" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Thu0230")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Thu0230" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Fri0230")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Fri0230" <%= msg %>></td>
				</tr>
				<tr>
					<td>3:00-3:30PM</td>
					<% msg = new String(); if(checkedTimes.contains("Mon0300")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Mon0300" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Tue0300")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Tue0300" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Wed0300")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Wed0300" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Thu0300")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Thu0300" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Fri0300")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Fri0300" <%= msg %>></td>
				</tr>
				<tr>
					<td>3:30-4:00PM</td>
					<% msg = new String(); if(checkedTimes.contains("Mon0330")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Mon0330" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Tue0330")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Tue0330" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Wed0330")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Wed0330" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Thu0330")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Thu0330" <%= msg %>></td>
					<% msg = new String(); if(checkedTimes.contains("Fri0330")) { msg = "checked"; } %>
					<td><input type="checkbox" name="times" value="Fri0330" <%= msg %>></td>
				</tr>
			</table>

		<INPUT TYPE="Submit" VALUE="Update"> 
	<%
	     }
             else
	     {
	 %>
		<p>Sorry, there is no student matching the data that was provided.</p>
         <%	
	     }
	%>
     </form>  	

      <a class="topOfPage" href="#top" title="Go to the top of this page">^ TOP</a>
    
    <!-- ##### Footer ##### -->
    <div id="footer">
        E-mail:&nbsp;<a href="./index.html" title="Email webmaster">jeffrey.s.garrett@gmail.com</a>
    </div>
  </body>
</html>
